﻿/*
 * RegExpress
 * 
 * Copyright (c) 2010, Daniel McGaughran
 * 
 * Licensed under the Apache Licence, Version 2.0 (the "Licence");
 * you may not use this file except in compliance with the Licence.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 * 
 */

using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;


namespace RegExpressModel.Entities
{
	/// <summary>
	/// Encapsulates a single match result of a regular expression.
	/// This is intended for use with potentially multiple matches (e.g. FindMatches), with captured groups.
	/// </summary>
	public class SingleMatchResult
	{

		public SingleMatchResult()
		{
			CapturedGroups = new List<GroupCapture>();
			MatchResult = Match.Empty;
		}


		public MatchResultType ResultStatus { get; private set; }
		public Match MatchResult { get; private set; }
		public List<GroupCapture> CapturedGroups { get; private set; }


		internal void SetResult(Match match, Regex regexObject)
		{
			MatchResult = match;
			ResultStatus = match.Success ? MatchResultType.Matches : MatchResultType.NoMatch;

			CapturedGroups.Clear();
			if (!match.Success) return; //No captured groups to list

			int[] indexes = regexObject.GetGroupNumbers();
			foreach (int i in indexes)
			{
				GroupCapture currentGroup = new GroupCapture()
				{
					GroupNumber = i,
					GroupName = regexObject.GroupNameFromNumber(i),
					Value = MatchResult.Groups[i].Value
				};
				CapturedGroups.Add(currentGroup);
			}
		}


		public void Reset()
		{
			ResultStatus = MatchResultType.Unknown;
			MatchResult = Match.Empty;
			CapturedGroups.Clear();
		}

	
	}
}